package preprocessing;

import weka.core.Attribute;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.ReplaceMissingValues;

public class replacemissingvalue {
    public static void printAttribute(Instances instances)
    {
        int numOfAttributes = instances.numAttributes();
        for(int i = 0; i < numOfAttributes ;++i)
        {
            Attribute attribute = instances.attribute(i);
            System.out.print(attribute.name() + " ");
        }
        System.out.println();
        //打印实例
        int numOfInstance = instances.numInstances();
        for(int i = 0; i < numOfInstance; ++i)
        {
            Instance instance = instances.instance(i);
            System.out.print(instance.toString() + " " + "\n");
        }
    }

    public static void main(String[] args) throws Exception {
        DataSource source = new DataSource("/home/tang/appdata/weka-3-8-4/data/labor.arff");
        Instances instances= source.getDataSet();
        ReplaceMissingValues rmv = new ReplaceMissingValues();
        rmv.setInputFormat(instances);
        Instances newInstances = Filter.useFilter(instances, rmv);
        System.out.println("数据缺失值处理完成,结果如下：");
        printAttribute(newInstances);
    }
}
